package com.jiayi.cms.service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.doudou.core.container.ArrUtil;
import com.doudou.core.container.ListUtil;
import com.jiayi.api.JiayiProductTypeService;
import com.jiayi.common.utils.PageUtils;
import com.jiayi.common.utils.poi.ExcelUtil;
import com.jiayi.data.entity.response.JiayiProductTypeDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jiayi.common.core.page.TableDataInfo;
import com.jiayi.cms.mapper.JiayiProductMapper;
import com.jiayi.data.entity.schema.JiayiProduct;
import com.jiayi.data.entity.excel.JiayiProductExcel;
import com.jiayi.data.entity.bo.JiayiProductBO;
import com.jiayi.data.entity.request.JiayiProductRequest;
import com.jiayi.data.entity.request.JiayiProductSearchRequest;
import com.jiayi.data.entity.request.JiayiProductSearchPageRequest;
import com.jiayi.data.entity.response.JiayiProductDTO;
import com.jiayi.data.converter.JiayiProductConverter;
import com.jiayi.api.JiayiProductService;
import com.jiayi.common.developutil.ConvertUtils;
import com.github.pagehelper.PageInfo;
import com.jiayi.common.utils.DateUtils;
import javax.servlet.http.HttpServletResponse;
import com.jiayi.common.constant.HttpStatus;
import static com.jiayi.common.utils.SecurityUtils.getUserIdStr;

/**
 * 产品展示Service业务层处理
 *
 * @author zzx
 * @date 2024-11-26
 */
@Service
@Slf4j
public class JiayiProductServiceImpl implements JiayiProductService {
    @Autowired
    private JiayiProductMapper jiayiProductMapper;

    @Autowired
    private JiayiProductConverter jiayiProductconverter;

    @Autowired
    private JiayiProductTypeService jiayiProductTypeService;

    /**
     * 查询产品展示
     *
     * @param id 产品展示主键
     * @return 产品展示
     */
    @Override
    public JiayiProductDTO selectJiayiProductById(Long id) {
        JiayiProduct jiayiProduct = jiayiProductMapper.selectJiayiProductById(id);
        JiayiProductDTO jiayiProductDTO = jiayiProductconverter.convertToJiayiProductDTO(jiayiProduct);
        jiayiProductDTO.setTypeDTO(jiayiProductTypeService.selectJiayiProductTypeById(jiayiProduct.getTypeId()));
        return jiayiProductDTO;
    }

    /**
     * 查询产品展示列表
     *
     * @param request 产品展示
     * @return 产品展示
     */
    @Override
    public TableDataInfo selectJiayiProductList(JiayiProductSearchPageRequest request) {
        PageUtils.startPage(request.getPageNum(),request.getPageSize());
        JiayiProduct jiayiProduct = jiayiProductconverter.convertToJiayiProduct(request);
        List<JiayiProduct> list = jiayiProductMapper.selectJiayiProductList(jiayiProduct);
        List<JiayiProductDTO> dtos = ConvertUtils.convert(list, jiayiProductconverter::convertToJiayiProductDTO);
        if (ListUtil.isNotEmpty(dtos)){
            List<Long> typeIds = dtos.stream().map(JiayiProductDTO::getTypeId).distinct().collect(Collectors.toList());
            List<JiayiProductTypeDTO> jiayiProductTypeDTOS = jiayiProductTypeService.selectJiayiProductTypeListByIds(ArrUtil.toArray(typeIds, Long.class));
            Map<Long, JiayiProductTypeDTO> typeDTOMap = jiayiProductTypeDTOS.stream().collect(Collectors.toMap(JiayiProductTypeDTO::getId, o -> o));
            for (JiayiProductDTO dto : dtos) {
                dto.setTypeDTO(typeDTOMap.get(dto.getTypeId()));
            }
        }
        return TableDataInfo.builder().code(HttpStatus.SUCCESS).msg("查询成功").rows(dtos).total(new PageInfo(list).getTotal()).build();
    }

    /**
     * 查询产品展示列表 不分页
     *
     * @param request 产品展示
     * @return 产品展示集合
     */
    public List<JiayiProduct> selectJiayiProductList(JiayiProductSearchRequest request){
        JiayiProduct jiayiProduct = jiayiProductconverter.convertToJiayiProduct(request);
        List<JiayiProduct> list = jiayiProductMapper.selectJiayiProductList(jiayiProduct);
        return list;
    }

    /**
     * excel 导出 全量
     *
     * @param request 产品展示
     */
    public void export(JiayiProductSearchRequest request, HttpServletResponse response){
        JiayiProduct jiayiProduct = jiayiProductconverter.convertToJiayiProduct(request);
        List<JiayiProduct> list = jiayiProductMapper.selectJiayiProductList(jiayiProduct);
        List<JiayiProductExcel> excelList = ConvertUtils.convert(list, jiayiProductconverter::convertToJiayiProductExcel);
        ExcelUtil<JiayiProductExcel> util = new ExcelUtil<JiayiProductExcel>(JiayiProductExcel.class);
        util.exportExcel(response, excelList, "产品展示数据");
    }

    /**
     * 新增产品展示
     *
     * @param request 产品展示
     * @return 结果
     */
    @Override
    public int insertJiayiProduct(JiayiProductRequest request) {
        JiayiProduct jiayiProduct = jiayiProductconverter.convertToJiayiProduct(request);
        jiayiProduct.setCreateBy(getUserIdStr());
        jiayiProduct.setCreateTime(DateUtils.getNowDate());
        return jiayiProductMapper.insertJiayiProduct(jiayiProduct);
    }

    /**
     * 修改产品展示
     *
     * @param request 产品展示
     * @return 结果
     */
    @Override
    public int updateJiayiProduct(JiayiProductRequest request) {
        JiayiProduct jiayiProduct = jiayiProductconverter.convertToJiayiProduct(request);
        return jiayiProductMapper.updateJiayiProduct(jiayiProduct);
    }

    /**
     * 批量删除产品展示
     *
     * @param ids 需要删除的产品展示主键
     * @return 结果
     */
    @Override
    public int deleteJiayiProductByIds(Long[] ids) {
        return jiayiProductMapper.deleteJiayiProductByIds(ids);
    }

    /**
     * 删除产品展示信息
     *
     * @param id 产品展示主键
     * @return 结果
     */
    @Override
    public int deleteJiayiProductById(Long id) {
        return jiayiProductMapper.deleteJiayiProductById(id);
    }
}
